package com.cqx.collection.persistence.entity;

import com.cqx.collection.util.UUIDUtil;
import org.springframework.util.StringUtils;

import javax.persistence.*;

/**
 * 所有实体类的基类。
 * @author chen.ping
 * @since 2.0
 */
@MappedSuperclass
public abstract class BaseEntity {

    public BaseEntity() {
        this.version = 0;
        long currentTimeMillis = System.currentTimeMillis();
        this.createDateTime = currentTimeMillis;
        this.updateDateTime = currentTimeMillis;
        this.isDelete = false;
    }

    // 内部使用的逻辑主键
    @Id
    @Column(length = 32)
    private String id;

    // 该条记录的版本号，乐观锁机制
    @Version
    private Integer version;
//
    // 创建时间
    @Column(name = "create_date_time")
    private Long createDateTime;

    // 更新时间
    @Column(name = "update_date_time")
    private Long updateDateTime;

    // 是否删除
    @Column(name = "is_delete")
    private Boolean isDelete;

    /**
     * 自动设置主键。
     */
    @PrePersist
    public void autoFillId() {
        if (StringUtils.isEmpty(id)) {
            id = UUIDUtil.uuidReplace();
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Long getCreateDateTime() {
        return createDateTime;
    }

    public void setCreateDateTime(Long createDateTime) {
        this.createDateTime = createDateTime;
    }

    public Long getUpdateDateTime() {
        return updateDateTime;
    }

    public void setUpdateDateTime(Long updateDateTime) {
        this.updateDateTime = updateDateTime;
    }

    public Boolean getDelete() {
        return isDelete;
    }

    public void setDelete(Boolean delete) {
        isDelete = delete;
    }
}
